package com.example.minefieldkeepercloud.view.pop;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import com.example.minefieldkeepercloud.R;
import com.example.minefieldkeepercloud.adapter.FactoryUserPopAdapter;
import com.example.minefieldkeepercloud.bean.FactoryUserBean;
import com.example.minefieldkeepercloud.interfac.ApiHttpCallback;
import com.example.minefieldkeepercloud.network.MyGson;
import com.example.minefieldkeepercloud.network.NetHelperNew;

import java.util.ArrayList;
import java.util.List;

public class FactoryUserPop extends PopupWindow implements View.OnClickListener {

    private Activity context;
    private View view;
    private OnSelectListener onSelectListener;
    private FactoryUserPopAdapter factoryPopAdapter;
    private String id;
    private List<FactoryUserBean.DataBean> listData;
    public FactoryUserPop(Context context, String id,OnSelectListener onSelectListener) {
        super(context);
        this.context = (Activity) context;
        this.id=id;
        this.onSelectListener=onSelectListener;
        initPop();

        initView();

    }

    private void initView() {
        View v=view.findViewById(R.id.view_null);
        v.setOnClickListener(this);
        ListView lv_houselayout=view.findViewById(R.id.lv_houselayout);
        listData=new ArrayList<>();
        factoryPopAdapter = new FactoryUserPopAdapter(context,listData);
        lv_houselayout.setAdapter(factoryPopAdapter);

        lv_houselayout.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if(listData.get(i).getFullName().equals("全部"))
                {
                    onSelectListener.select(-1,listData.get(i).getFullName());
                }
                else
                {
                    onSelectListener.select(listData.get(i).getID(),listData.get(i).getFullName());
                }

                 factoryPopAdapter.setSelected(i);
                dismiss();
            }
        });
        getNetData();
    }
    private void getNetData()
    {
        NetHelperNew.GetTGByFactoryID(id, new ApiHttpCallback(context,false) {
            @Override
            public void onSuccess(String data) {
                FactoryUserBean factoryUserBeans = MyGson.getInstance().fromJson(data, FactoryUserBean.class);
                if(factoryUserBeans.getType()==1)
                {
                    FactoryUserBean.DataBean dataBean =new FactoryUserBean.DataBean();
                    dataBean.setFullName("全部");
                    listData.add(dataBean);
                    listData.addAll(factoryUserBeans.getData());
                    factoryPopAdapter.setListData(listData);
                    factoryPopAdapter.notifyDataSetChanged();
                }
            }
        });

    }


    /**
     * 初始化pop样式
     */
    private void initPop() {

        view= LayoutInflater.from(context).inflate(R.layout.pop_factory,null);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);


        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        this.setContentView(view);


        ColorDrawable dw = new ColorDrawable(context.getResources().getColor(R.color.gray_bg));
        //设置SelectPicPopupWindow弹出窗体的背景
        setBackgroundDrawable(dw);

        //获取popwindow焦点
        setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        setOutsideTouchable(true);
    }

    /**\
     * 设置透明背景
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        context.getWindow().setAttributes(lp);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {

            case R.id.view_null:
                //点击半透明
                dismiss();
                break;

        }
    }

    /**
     * 房型选择回调
     */
    public  interface OnSelectListener
    {
        public void select(int id,String name);//不限

    }
}
